﻿namespace AC.ExtendedRenderer.Toolkit.Charting
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    [ToolboxItem(true), ToolboxBitmap(typeof(DataChart))]
    public class DataChart : UserControl
    {
        private ArrayList _arrayList;
        private AC.ExtendedRenderer.Toolkit.Charting.ChartType _chartType;
        private Color _colorGrid;
        private Color _colorLine;
        private int _gridPixel;
        private int _yMaxInit;
        private Container components;

        public DataChart()
        {
            this.InitializeComponent();
            this.BackColor = Color.Silver;
            this._colorLine = Color.DarkBlue;
            this._colorGrid = Color.Yellow;
            this._yMaxInit = 0x3e8;
            this._gridPixel = 0;
            this._chartType = AC.ExtendedRenderer.Toolkit.Charting.ChartType.Stick;
            this._arrayList = new ArrayList();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            base.Name = "DataChart";
            base.Size = new Size(150, 0x10);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int count = this._arrayList.Count;
            if (count != 0)
            {
                int num7;
                int num8;
                double num2 = 0.0;
                double initialHeight = this.InitialHeight;
                for (int i = 0; i < count; i++)
                {
                    num2 = Convert.ToDouble(this._arrayList[i]);
                    if (num2 > initialHeight)
                    {
                        initialHeight = num2;
                    }
                }
                Rectangle clientRectangle = base.ClientRectangle;
                num2 = (initialHeight == 0.0) ? 1.0 : (((double) clientRectangle.Height) / initialHeight);
                int width = clientRectangle.Width;
                int height = clientRectangle.Height;
                Pen pen = null;
                e.Graphics.Clear(this.BackColor);
                if (this.GridPixels != 0)
                {
                    pen = new Pen(this.GridColor, 1f);
                    num7 = clientRectangle.Width / this.GridPixels;
                    num8 = clientRectangle.Height / this.GridPixels;
                    for (int j = 1; j <= num7; j++)
                    {
                        e.Graphics.DrawLine(pen, j * this.GridPixels, 0, j * this.GridPixels, height);
                    }
                    for (int k = 1; k < num8; k++)
                    {
                        e.Graphics.DrawLine(pen, 0, k * this.GridPixels, width, k * this.GridPixels);
                    }
                }
                if (this.ChartType == AC.ExtendedRenderer.Toolkit.Charting.ChartType.Stick)
                {
                    pen = new Pen(this.LineColor, 2f);
                    for (int m = count - 1; m >= 0; m--)
                    {
                        num7 = width - (2 * (count - m));
                        if (num7 <= 0)
                        {
                            break;
                        }
                        num8 = height - ((int) (num2 * Convert.ToDouble(this._arrayList[m])));
                        e.Graphics.DrawLine(pen, num7, height, num7, num8);
                    }
                }
                else if (this.ChartType == AC.ExtendedRenderer.Toolkit.Charting.ChartType.Line)
                {
                    pen = new Pen(this.LineColor, 1f);
                    int num12 = width - 2;
                    int num13 = height - ((int) (num2 * Convert.ToDouble(this._arrayList[count - 1])));
                    for (int n = count - 2; n >= 0; n--)
                    {
                        num7 = width - (2 * (count - n));
                        if (num7 <= 0)
                        {
                            break;
                        }
                        num8 = height - ((int) (num2 * Convert.ToDouble(this._arrayList[n])));
                        e.Graphics.DrawLine(pen, num12, num13, num7, num8);
                        num12 = num7;
                        num13 = num8;
                    }
                }
                base.OnPaint(e);
                pen.Dispose();
            }
        }

        public void UpdateChart(double d)
        {
            int num = base.ClientRectangle.Width / 2;
            if (this._arrayList.Count >= num)
            {
                this._arrayList.RemoveAt(0);
            }
            this._arrayList.Add(d);
            base.Invalidate();
        }

        [Category("DataChart"), Description("Gets or sets the current chart Type for stick or Line")]
        public AC.ExtendedRenderer.Toolkit.Charting.ChartType ChartType
        {
            get
            {
                return this._chartType;
            }
            set
            {
                this._chartType = value;
            }
        }

        [Category("DataChart"), Description("Gets or sets the current Grid Color in chart")]
        public Color GridColor
        {
            get
            {
                return this._colorGrid;
            }
            set
            {
                this._colorGrid = value;
            }
        }

        [Description("Enables grid drawing with spacing of the Pixel number"), Category("DataChart")]
        public int GridPixels
        {
            get
            {
                return this._gridPixel;
            }
            set
            {
                this._gridPixel = value;
            }
        }

        [Category("DataChart"), Description("Gets or sets the initial maximum Height for sticks in chart")]
        public int InitialHeight
        {
            get
            {
                return this._yMaxInit;
            }
            set
            {
                this._yMaxInit = value;
            }
        }

        [Description("Gets or sets the current Line Color in chart"), Category("DataChart")]
        public Color LineColor
        {
            get
            {
                return this._colorLine;
            }
            set
            {
                this._colorLine = value;
            }
        }
    }
}

